package gold.digger;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by fanzhenyu02 on 2020/3/1.
 */
public class LC17 {
    public static List<String> arr2 = Arrays.asList("a", "b", "c");
    public static List<String> arr3 = Arrays.asList("d", "e", "f");
    public static List<String> arr4 = Arrays.asList("g", "h", "i");
    public static List<String> arr5 = Arrays.asList("j", "k", "l");
    public static List<String> arr6 = Arrays.asList("m", "n", "o");
    public static List<String> arr7 = Arrays.asList("p", "q", "r", "s");
    public static List<String> arr8 = Arrays.asList("t", "u", "v");
    public static List<String> arr9 = Arrays.asList("w", "x", "y", "z");
    public static Map<Character, List<String>> dict = new TreeMap<>();

    static {
        dict.put('2', arr2);
        dict.put('3', arr3);
        dict.put('4', arr4);
        dict.put('5', arr5);
        dict.put('6', arr6);
        dict.put('7', arr7);
        dict.put('8', arr8);
        dict.put('9', arr9);
    }

    public List<String> letterCombinations(String digits) {
        List<String> reList = new ArrayList<>();
        if (null == digits || digits.length() <= 0) {
            return reList;
        }

        List<String> charList = dict.get(digits.charAt(0));
        List<String> nextLetterList = letterCombinations(digits.substring(1));
        for (String firstChar : charList) {
            if (nextLetterList.isEmpty()) {
                reList.add(firstChar);
            } else {
                for (String subLetter : nextLetterList) {
                    reList.add(firstChar + subLetter);
                }
            }
        }

        return reList;
    }

    public static void main(String[] args) {
        LC17 an = new LC17();
        System.out.println(an.letterCombinations("23"));
    }
}
